#pragma rtGlobals=1		// Use modern global access method.
#include <Execute Cmd On List>

Function getWaveNote(wName)
String wName
String saveDF = getdatafolder(1), pathToWave = wName[0,Strsearch(wName, ":", inf, 1)]
String wave_Name = ReplaceString(pathToWave, wName, "")

Setdatafolder pathToWave

Wave w1 = $wave_Name

String wInfo = note(w1)

Variable lowEnergy = NumberByKey("Low Energy", wInfo, "=", "\r"), HighEnergy = NumberByKey("High Energy", wInfo, "=", "\r"), EnergyStep =  NumberByKey("Energy Step", wInfo, "=", "\r")
Variable Sweeps =  NumberByKey("Number of Sweeps", wInfo, "=", "\r"), StepTime =  NumberByKey("Step Time", wInfo, "=", "\r")/100
String scanDate = StringByKey("Date", wInfo, "=", "\r"), scanTime = StringByKey("Time", wInfo, "=", "\r")

if(stringMatch(getDataFolder(0), "Processed_Data") == 0)
	NewDataFolder/O/S Processed_Data
	Duplicate/O w1, $wave_Name
endif

WAVE w2 = $wave_Name
SetScale/I x, LowEnergy, HighEnergy, "Binding Energy [eV]", w2
WaveStats/Q w2
SetScale/I y, V_min, V_max, "Counts/Sec", w2

w2 = w2/(Sweeps*StepTime)

String energyWave = "BE_" + wave_Name

Make/O/N=(numpnts(w2)) $energyWave = EnergyStep*x + LowEnergy

if(stringMatch(getDataFolder(0), "Processed_Data") == 1)
	//calPhotonEnergy("C_1s")
endif

SetDataFolder saveDF
end

Function walkDataFolders(mainDataFolder)
String mainDataFolder
String saveDF = getDataFolder(1)
String dfHolder, currentDF = mainDataFolder
SetDataFolder $mainDataFolder
Variable i = 0

Do
	dfHolder = StringFromList(i, dataFolderInfo(currentDF), ",")
	//Print StringFromList(i, dataFolderInfo(currentDF), ",") 
	//dataFolderInfo(dfHolder)
	
	if(strlen(dataFolderInfo(dfHolder)) != 0)
		//Print dfHolder
		walkDataFolders(dfHolder)
		
	else //if(StringMatch(dfHolder, "Processed_Data") == 0 && StringMatch(currentDF, "Processed_Data") == 0)
		Variable q = 0
		String listOfWaves = waveList("!BE*", ";", "")
		
		Do
			getWaveNote(StringFromList(q, listOfWaves, ";"))
			q += 1
		
		while(q < ItemsInList(listOfWaves, ";"))
		
	endif
		
	i += 1
while(i < ItemsInList(dataFolderInfo(currentDF), ","))

SetdataFolder saveDF

end

Function/S dataFolderInfo(dataFolder)
String dataFolder
String saveDF = getDataFolder(1)
//SetDataFolder $dataFolder

String subFolderList = stringByKey("FOLDERS", DataFolderDir(1))
SetDataFolder $saveDF
Return subFolderList

end

Function calPhotonEnergy(wName)
String wName
Wave w3 = $wName
Variable i = 0, j =0

WaveStats/Q w3
Variable photonEnergy = V_maxloc + 84
String listWaves = waveList("BE_*", ";", "")

if(strlen(listWaves) != 0)
Do
	String waveSelected = stringfromlist(i, listWaves, ";")
	WAVE w4 = $waveSelected
	
	Do
		w4[j] = photonEnergy - w4[j]
		j += 1
	while(j < numpnts(w4))
	
	j = 0
	i += 1
while(i < ItemsInList(listWaves, ";"))
endif

//KillVariables/A/Z
end

//WalkDataFoldersWithWaveCmd("root:GIZO_X24A:" , "CalPhotonEnergy(\"%s\")", "Au4f", "")

Function walkDataFoldersGraph(mainDataFolder, graphName)
String mainDataFolder, graphName
String saveDF = getDataFolder(1)
String dfHolder, currentDF = mainDataFolder
SetDataFolder $mainDataFolder
Variable i = 0, j = 0

Do
	dfHolder = StringFromList(i, dataFolderInfo(currentDF), ",")
	//Print StringFromList(i, dataFolderInfo(currentDF), ",") 
	//dataFolderInfo(dfHolder)
	
	if(strlen(dataFolderInfo(dfHolder)) != 0)
		//Print dfHolder
		walkDataFoldersGraph(dfHolder, graphName)
		
	else //if(StringMatch(dfHolder, "Processed_Data") == 0 && StringMatch(currentDF, "Processed_Data") == 0)
		String listOfWaves = waveList("*", ";", "")
		Do
			String y_wave = stringfromlist(j, listofWaves, ";")
			String x_wave = "BE_" + y_wave 
			WAVE w5 = $y_wave, w6 = $x_wave
			String y_wave_N = y_wave + "_N"
			Duplicate/O w5, $y_wave_N
			WAVE w7 = $y_wave_N
			WaveStats/Q w7
			w7 = (w7 - V_min)/(V_max - V_min)
			DoWindow $graphName
			//if( V_flag == 0)
			//	Display/N=$graphName w7 vs w6
			//else
			//	AppendToGraph/W=$graphName w7 vs w6
			//endif
			
			j += 1
		while(j < itemsinlist(listOfWaves, ";"))
		j = 0
	endif
	
	KillVariables/A/Z	
	i += 1
while(i < ItemsInList(dataFolderInfo(currentDF), ","))

SetdataFolder saveDF

end
